package menu.lt.pers.menu;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.google.gson.Gson;
import menu.lt.pers.menu.Bean.ResultByNameBean;
import menu.lt.pers.menu.adapter.RecipeListByNameViewAdapter;
import menu.lt.pers.menu.dao.ResourceDao;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class ResultByName extends FragmentActivity {
    private ListView listView;
    private String search_text;
    private RecipeListByNameViewAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_result);
        initView();
        setListener();
        initData();
        updateView();
    }

    private void initView() {
        listView = findViewById(R.id.listView);
    }

    private void initData() {
        search_text = getIntent().getStringExtra("search_text");
        adapter = new RecipeListByNameViewAdapter(this, search_text);
    }

    private void updateView() {
        listView.setAdapter(adapter);
    }

    private void setListener() {
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(ResultByName.this, RecipeInfo.class);
                Gson gson = new Gson();
                intent.putExtra("recipe", gson.toJson(adapter.getItem(position)));
                startActivity(intent);
            }
        });
    }
}